<?php 

Class wechatModel extends appModel
{
	//关键词回复
	public function key_reply($key)
	{
		$key_id=0;
		//完全匹配
		$sql="SELECT id  FROM ".$this->table('wxkeyword')." WHERE keyword='".$key."' AND type=0";
		$key_id=$this->getOne($sql);
		if($key_id=='')
		{
		   $sql="SELECT id,keyword FROM ".$this->table('wxkeyword')." WHERE  type=1";
		   $key_list=$this->getAll($sql);
		   foreach($key_list as $k=>$v)
		   {
			   if(strstr($key,$v['keyword']))
			  {
				$key_id=$v['id'];
			   }
		   }
		}
		
		
		if($key_id!='')
		{ 
		   $sql="SELECT b.type,b.id FROM ".$this->table('wxkeyword')." as a,".$this->table('rule')." as b WHERE b.id=a.rid AND a.status=1 AND a.id='".$key_id."'";
		  
		   $keyword=$this->getRow($sql);
		   
		   $result='';
		   if($keyword['type']==1)
		   {
		   	$sql="SELECT content FROM ".$this->table('basic_reply')." WHERE rid='".$keyword['id']."'";
		   	$result=$this->getOne($sql);
		   }
		   else
		   {
		   	$sql="SELECT title,description,thumb,url FROM ".$this->table('news_reply')." WHERE rid='".$keyword['id']."'";
		   	$result=$this->getAll($sql);
		   }
		}      
		
		return $result;
	}
	public function get_msg($id)
	{
		
		$sql="SELECT type FROM ".$this->table('rule')." WHERE id='".$id."'";
		$type=$this->getOne($sql);
		$result='';
		
		if($type==1)
		{
			$sql="SELECT content FROM ".$this->table('basic_reply')." WHERE rid='".$id."'";
			$result=$this->getOne($sql);
		}
		else 
		{
			$sql="SELECT title,description,thumb,url FROM ".$this->table('news_reply')." WHERE rid='".$id."'";
			$result=$this->getAll($sql);
		}
		return $result;
	}
    function is_vaild()
    {
    	$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='wxvaild'";
    	$is_vaild=$this->getOne($sql);
    	return  $is_vaild;
    }
	function getToken()
	{
		$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='token'";
		$is_vaild=$this->getOne($sql);
		return  $is_vaild;
	}
    function update_vaild()
    {
    	$sql="UPDATE ".$this->table('options')." SET op_value = 1 WHERE op_key='wxvaild'";
    	return $this->query($sql);
    }
    function get_dreply()
    {
    	$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='wxdreply'";
    	$areply_id=$this->getOne($sql);
    	 
    	$areply_content='';
    	if($areply_id)
    	{
    		$sql="SELECT type FROM ".$this->table('rule')." WHERE id='".$areply_id."'";
    		$rule_type=$this->getOne($sql);
    		if($rule_type==1)
    		{
    			$sql="SELECT content FROM ".$this->table('basic_reply')." WHERE rid='".$areply_id."'";
    			$areply_content=$this->getOne($sql);
    		}
    		elseif($rule_type==2)
    		{
    			$sql="SELECT * FROM ".$this->table('news_reply')." WHERE rid='".$areply_id."'";
    			$areply_content=$this->getAll($sql);
    		}
    	}
    	return $areply_content;
    }
    function get_areply()
    {
    	$sql="SELECT op_value FROM ".$this->table('options')." WHERE op_key='wxareply'";
    	$areply_id=$this->getOne($sql);
    	
    	$areply_content='';
    	if($areply_id)
    	{
    		$sql="SELECT type FROM ".$this->table('rule')." WHERE id='".$areply_id."'";
    		$rule_type=$this->getOne($sql);
    		if($rule_type==1)
    		{
    			$sql="SELECT content FROM ".$this->table('basic_reply')." WHERE rid='".$areply_id."'";
    			$areply_content=$this->getOne($sql);
    		}
    		elseif($rule_type==2)
    		{
    			$sql="SELECT * FROM ".$this->table('news_reply')." WHERE rid='".$areply_id."'";
    			$areply_content=$this->getAll($sql);
    		}
    	}
    	return $areply_content;
    }
    
    function save_user($openid)
    {
    	$sql="SELECT count(*) FROM ".$this->table('wxusers')." WHERE openid='".$openid."'";
    	$user_num=$this->getOne($sql);
    	
    	if($user_num>0)
    	{
    		$sql="UPDATE ".$this->table('wxusers')." SET is_follow='1'  WHERE openid='".$openid."'";
    		$user_num=$this->query($sql);
    	}
    	else 
    	{
    		$sql="INSERT INTO ".$this->table('wxusers')."(openid,createtime) VALUES('".$openid."','".$_SERVER['REQUEST_TIME']."')";
    		
    		
    		$user_num=$this->query($sql);
    	}
    }
    
    function cancel_follow($openid)
    {
    	$sql="UPDATE ".$this->table('wxusers')." SET is_follow='0'  WHERE openid='".$openid."'";
    	$user_num=$this->query($sql);
    }
    
	
	
}